package com.ldcc.report_system.ui.setting;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

import com.ldcc.report_system.ApplicationConfig;
import com.ldcc.report_system.ReportApplication;
import com.ldcc.report_system.databinding.ActivityNetWorkSettingBinding;
import com.ldcc.report_system.ui.LoginActivity;
import com.ldcc.report_system.util.ActivitySkipUtil;
import com.ldcc.report_system.util.AlertUtil;
import com.ldcc.report_system.util.ServerUtil;
import com.ldcc.report_system.util.SharedPreferencesUtils;
import com.ldcc.report_system.util.WindowUtils;

/**
 * @author 登录界面网络设置
 * @description 网络设置界面
 */
public class NetWorkSettingActivity extends AppCompatActivity {
    public static final String TAG = "NetWorkSettingActivity";
    private ActivityNetWorkSettingBinding netWorkSettingBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.netWorkSettingBinding = ActivityNetWorkSettingBinding.inflate(getLayoutInflater());
        setContentView(this.netWorkSettingBinding.getRoot());
        // 设置界面顶部为透明
        WindowUtils.setWindowTopTransparent(getWindow());
        this.initPage();
    }

    /**
     * @description 初始化界面
     */
    private void initPage() {

        // 初始化数值
        String serverAddress = (String) SharedPreferencesUtils.getParam(ReportApplication.getContext(), ApplicationConfig.SERVER_ADDRESS_KEY, "");
        String port = (String) SharedPreferencesUtils.getParam(ReportApplication.getContext(), ApplicationConfig.SERVER_PORT_KEY, "");
        this.netWorkSettingBinding.serverAddress.setText(serverAddress);
        this.netWorkSettingBinding.serverPort.setText(port);
        this.netWorkSettingBinding.saveBtn.setOnClickListener(view -> {
            String address_save = this.netWorkSettingBinding.serverAddress.getText().toString();
            String port_save = this.netWorkSettingBinding.serverPort.getText().toString();
            // todo:判断是否合法
            // 保存
            ServerUtil.saveServerUrl(address_save, port_save);
            AlertUtil.shortTime("网络配置保存成功");
            // 跳转login
            ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
        });
    }

    /**
     * @description 返回键事件, 返回, 选择日报界面
     */
    @Override
    public void onBackPressed() {
        ActivitySkipUtil.skipAnotherActivity(this, LoginActivity.class);
    }
}